package controller;

import dao.ItemJpaController;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import dao.exceptions.NonexistentEntityException;
import model.Item;
import model.Questao;
import pagecontroller.JsfUtil;
import pagecontroller.PageManager;

/**
 *
 * @author Edilson Fernandes da Costa
 * @version 1.0
 */
public class ItemController {

    public ItemController() {
        if (JsfUtil.getObjectFromSession("ItemJpaController") == null) {
            jpaController = new ItemJpaController();
        } else {
            jpaController = (ItemJpaController) JsfUtil.getObjectFromSession("ItemJpaController");
        }
    }
    private Item item = null;
    private Item itemAux = null;
    private ItemJpaController jpaController = null;
    private List<Item> items = null;


   public String persiste() {
        if (item.getId() == null) {
            return create();
        } else {
            return update();
        }
    }

    public String update() {
        try {
            jpaController.edit(this.item);
            itemAux = item;
            this.item = null;
            JsfUtil.addSuccessMessage("Item Atualizado com Sucesso");
            getPageManager().setPageItemRelat();
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "refreshPage";
    }

    public String create() {
        try {
            jpaController.create(item);
            itemAux = item;
            item = null;
            JsfUtil.addSuccessMessage("Item cadastrado com sucesso.");
            getPageManager().setPageItemRelat();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "Ocorreu um erro na percistencia dos dados.");
            return null;
        }
        return "refreshPage";
    }

    public String edit() {
        Item itemTemp = (Item) JsfUtil.getObjectFromRequestParameter("itemTemp");
        this.item = itemTemp;
        return "refreshPage";
    }

    public String load() {
        itemAux = (Item) JsfUtil.getObjectFromRequestParameter("itemTemp");
        getPageManager().setPageItemRelat();
        return "refreshPage";
    }


    public String destroy() {
        Item itemTemp = (Item) JsfUtil.getObjectFromRequestParameter("item");
        try {
            jpaController.destroy(itemTemp.getId());
            JsfUtil.addSuccessMessage("Item removido com sucesso.");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }


    public String editButton() {
        this.item = itemAux;
        getPageManager().setPageItemCad();

        return "refreshPage";
    }

    public String destroyButton() {
        Item itemTemp = itemAux;
        try {
            jpaController.destroy(itemTemp.getId());
            JsfUtil.addSuccessMessage("Item removida com sucesso.");
            itemAux = null;

        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AlunoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }


    private PageManager getPageManager() {
        PageManager pageManager = (PageManager) JsfUtil.getObjectFromSession("PageManager");
        return pageManager;
    }



    public Item getItem() {
        if (item == null) {
            item = new Item();
        }
        return item;
    }

    public List<Item> getItems(){
        this.items = jpaController.findItemEntities();
        return this.items;
    }

    public List<Item> getItems(Questao questao){
        this.items = jpaController.findItemEntities(questao);
        return this.items;
    }

    
    public ItemJpaController getJpaController() {
        return jpaController;
    }

    public Item getItemAux() {
        return itemAux;
    }


}
